package com.allin.mall.common.core.api;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult <T> {

    private Integer code;

    private String message;

    private T data;

    private CommonResult(IResultCode code, T data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }
    /**
     * 成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(ResultCodeEnum.SUCCESS, data);
    }

    public static <T> CommonResult<T> success() {
        return success(null);
    }

    /**
     * 失败
     * @param code
     * @return
     */
    public static CommonResult fail(IResultCode code) {
        return new CommonResult(code, null);
    }
}
